package com.westar.core.services;

import com.google.gson.Gson;
import com.westar.base.businessUtil.ImplementDetailUtil;
import com.westar.base.businessUtil.ImplementPageUtil;
import com.westar.base.cons.Constant;
import com.westar.base.exceptions.SelfHttpException;
import com.westar.base.model.*;
import com.westar.base.pojo.PageBean;
import com.westar.base.pojo.WestarResult;
import com.westar.base.util.TokenUtil;
import com.westar.base.util.WestarHttpUtil;
import com.westar.core.conf.SysConst;
import com.westar.core.dao.ImplementDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 实施清单service
 */
@Service
public class ImplementService {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ImplementDao implementDao;
    /**
     * 初始化区域信息
     */
    public void initImplement(String actCookie) throws IOException {
        //获取所有的区域信息
        List<Implement> listTreeImplement = implementDao.listTreeImplement(1);
        if(null == listTreeImplement || listTreeImplement.isEmpty()){
            //添加区域数据
            this.addImplement(actCookie);
        }
    }

    /**
     * 添加区域信息
     * @param actCookie
     */
    private void addImplement(String actCookie) throws IOException {
        List<Implement> areasList = ImplementPageUtil.queryAllImplement(actCookie);
        if(null != areasList && !areasList.isEmpty()){
            for (Implement implement:  areasList) {
                implementDao.add(implement);
            }
        }
    }

    /**
     * 从省一体化平台获取数据信息
     * @param eventCode
     * @param actCookie
     * @param areaCode
     * @return
     */
    public ImplementDetail getImplementDetailFromUp(String eventCode,
                                                    String eventName,
                                                    String actCookie,
                                                    String areaCode,
                                                    String isNotPublicServices){

        //分页获取实施清单信息
        PageBean<ImplementDetail> pageBean = null;
        try {
            if(StringUtils.isNotEmpty(eventCode)){
                ImplementDetail implementDetail = new ImplementDetail();
                implementDetail.setAreaCode(areaCode);
                implementDetail.setShowEventCode(eventCode);

                pageBean = ImplementPageUtil.queryAllImplementDetail(actCookie,1,100,implementDetail,isNotPublicServices);
            }
            if((null == pageBean  || pageBean.getTotalCount() != 1) && StringUtils.isNotEmpty(eventName)){
                ImplementDetail implementDetail = new ImplementDetail();
                implementDetail.setAreaCode(areaCode);
                implementDetail.setEventName(eventName);
                pageBean = ImplementPageUtil.queryAllImplementDetail(actCookie,1,100,implementDetail,isNotPublicServices);
            }

            if(null == pageBean  || pageBean.getTotalCount() != 1){
                return null;
            }
            Integer totalNum = pageBean.getTotalCount();
            if( totalNum == 1 ){
                return pageBean.getRecordList().get( 0);
            }else{
                return null;
            }
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 通过管理员取得实施清单数据
     * @param eventCode 实施清单
     * @param areaCode 区域代码
     * @return
     */
    public ImplementDetail getImplementDetailFromUpByAdmin(String eventCode, String areaCode,String isNotPublicServices) {
        UserInfo adminUser = userInfoService.getUserInfoByAccount(SysConst.ADMIN_ACCOUNT);
        String token = null;
        try {
            token = TokenUtil.initToken(adminUser.getAccount(),adminUser.getPasswd(), Constant.MODEL_THINGSUI);
            String actCookie = TokenUtil.getCookie(token,Constant.MODEL_THINGSUI);

            ImplementDetail implementDetail = this.getImplementDetailFromUp(eventCode,null,actCookie,areaCode,isNotPublicServices);
            return implementDetail;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (SelfHttpException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 分页事项的查询详情信息
     * @param actCookie
     * @return
     */
    public WestarResult initImplementForDetailList(String actCookie,String areaCode,Integer pageNum, Integer pageSize,
                                                   String  showEventCode,String deptCodes,String isNotPublicServices) throws IOException {
        ImplementDetail implementDetailParam = new ImplementDetail();
        implementDetailParam.setAreaCode(areaCode);
        implementDetailParam.setShowEventCode(showEventCode);
        implementDetailParam.setDeptCodes(deptCodes);
        //1、查询目录表
        PageBean<ImplementDetail> queryAllImplementDetail = ImplementPageUtil.queryAllImplementDetail(actCookie,pageNum,pageSize,implementDetailParam,isNotPublicServices);

        List<ImplementDetail> implementDetailList = queryAllImplementDetail.getRecordList();

        PageBean<WtaskGeneralBasic> pageBean = new PageBean<WtaskGeneralBasic>();
        pageBean.setTotalCount(queryAllImplementDetail.getTotalCount());

        List<WtaskGeneralBasic> wtaskGeneralBasicList = new ArrayList<>();
        if(null != implementDetailList && !implementDetailList.isEmpty()){
            for (ImplementDetail implementDetail:  implementDetailList) {

                WtaskGeneralBasic wtaskGeneralBasic = ImplementPageUtil.constrWtaskGeneralBasic(implementDetail,actCookie);
                wtaskGeneralBasicList.add(wtaskGeneralBasic);

            }
        }
        pageBean.setRecordList(wtaskGeneralBasicList);
        Gson gson = new Gson();
        return WestarResult.ok(gson.toJson(pageBean));
    }
}
